using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using BTOps;

public partial class BTOps_ReceiveLocation : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void ReceiveLocationDetailsDS_Updating(object sender, ObjectDataSourceMethodEventArgs e)
    {
        BTReceiveLocation receiveLocation = (BTReceiveLocation) e.InputParameters["receiveLocation"];
        receiveLocation.ApplicationName = Request.QueryString["app"];
        receiveLocation.TransportInfo = GenerateTransportInfo(receiveLocation.TransportType);
    }

    protected void ReceiveLocationDetailsDS_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
    {
        BTReceiveLocation receiveLocation = (BTReceiveLocation) e.InputParameters["receiveLocation"];
        if (null != receiveLocation)
        {
            TextBox PortName = dvwReceiveLocation.FindControl("txtName") as TextBox;
            DropDownList TransportType = dvwReceiveLocation.FindControl("ddlTransportType") as DropDownList;
            TextBox Address = dvwReceiveLocation.FindControl("txtAddress") as TextBox;
            DropDownList ReceivePipeline = dvwReceiveLocation.FindControl("ddlReceivePipeline") as DropDownList;
            DropDownList SendPipeline = dvwReceiveLocation.FindControl("ddlSendPipeline") as DropDownList;
            TextBox ReceiveHandler = dvwReceiveLocation.FindControl("ReceiveHandler") as TextBox;
            TextBox Description = dvwReceiveLocation.FindControl("txtDescription") as TextBox;

            receiveLocation.ApplicationName = Request.QueryString["app"];
            if (null != PortName)
                receiveLocation.Name = PortName.Text.ToUpper();
            if (null != TransportType)
                receiveLocation.TransportType = TransportType.SelectedValue;
            if (null != Address)
                receiveLocation.Address = Address.Text;
            if (null != SendPipeline)
                receiveLocation.SendPipelineName = SendPipeline.SelectedValue;
            if (null != ReceivePipeline)
                receiveLocation.ReceivePipelineName = ReceivePipeline.SelectedValue;
            if (null != ReceiveHandler)
                receiveLocation.ReceiveHandlerName = ReceiveHandler.Text;
            if (null != Description)
                receiveLocation.Description = Description.Text;

            receiveLocation.TransportInfo = GenerateTransportInfo(receiveLocation.TransportType);
        }
    }

    protected void dvwReceiveLocation_DataBound(object sender, EventArgs e)
    {
        if (null != dvwReceiveLocation.DataItem)
        {
            BTReceiveLocation receiveLocation = (BTReceiveLocation) (dvwReceiveLocation.DataItem);
            string transportTypeData = receiveLocation.TransportTypeData ?? String.Empty;

            switch (receiveLocation.TransportType)
            {
                case "FILE":
                    txtFolder.Text = Path.GetDirectoryName(receiveLocation.Address);
                    txtFileMask.Text = ReadTransportDataProperty(transportTypeData, "FileMask");
                    txtPollingInterval.Text = ReadTransportDataProperty(transportTypeData, "PollingInterval");
                    txtBatchSize.Text = ReadTransportDataProperty(transportTypeData, "BatchSize");
                    txtBatchSizeInBytes.Text = ReadTransportDataProperty(transportTypeData, "BatchSizeInBytes");
                    txtFileNetFailRetryInt.Text = ReadTransportDataProperty(transportTypeData, "FileNetFailRetryInt");
                    txtFileNetFailRetryCount.Text = ReadTransportDataProperty(transportTypeData, "FileNetFailRetryCount");
                    chkRemoveReceivedFiles.Checked = ReadTransportDataProperty(transportTypeData, "RemoveReceivedFiles") == "0";
                    txtRemoveReceivedFileDelay.Text = ReadTransportDataProperty(transportTypeData, "RemoveReceivedFileDelay");
                    txtRemoveReceivedFileRetryCount.Text = ReadTransportDataProperty(transportTypeData, "RemoveReceivedFileRetryCount");
                    txtRemoveReceivedFileMaxInterval.Text = ReadTransportDataProperty(transportTypeData, "RemoveReceivedFileMaxInterval");
                    break;
                case "FTP":
                    txtServerAddress.Text = ReadTransportDataProperty(transportTypeData, "serverAddress");
                    txtServerPort.Text = ReadTransportDataProperty(transportTypeData, "serverPort");
                    txtUserName.Text = ReadTransportDataProperty(transportTypeData, "userName");
                    txtPassword.Text = ReadTransportDataProperty(transportTypeData, "password");
                    txtTargetFolder.Text = ReadTransportDataProperty(transportTypeData, "targetFolder");
                    txtTargetFileName.Text = ReadTransportDataProperty(transportTypeData, "fileMask");
                    txtCommandLogFilename.Text = ReadTransportDataProperty(transportTypeData, "commandLogFilename");
                    ddlRepresentationType.SelectedValue = ReadTransportDataProperty(transportTypeData, "representationType");
                    txtSpoolingFolder.Text = ReadTransportDataProperty(transportTypeData, "spoolingFolder");
                    txtMaximumBatchSize.Text = ReadTransportDataProperty(transportTypeData, "maximumBatchSize");
                    txtMaximumNumberOfFiles.Text = ReadTransportDataProperty(transportTypeData, "maximumNumberOfFiles");
                    txtErrorThreshold.Text = ReadTransportDataProperty(transportTypeData, "errorThreshold");
                    txtMaxFileSize.Text = ReadTransportDataProperty(transportTypeData, "maxFileSize");
                    txtFirewallAddress.Text = ReadTransportDataProperty(transportTypeData, "firewallAddress");
                    txtFirewallPort.Text = ReadTransportDataProperty(transportTypeData, "firewallPort");
                    txtFirewallUser.Text = ReadTransportDataProperty(transportTypeData, "firewallUser");
                    txtFirewallPassword.Text = ReadTransportDataProperty(transportTypeData, "firewallPassword");
                    ddlFirewallType.SelectedValue = ReadTransportDataProperty(transportTypeData, "firewallType");
                    ddlPassiveMode.SelectedValue = (ReadTransportDataProperty(transportTypeData, "passiveMode") == "False") ? "Active" : "Passive";
                    ddlPollingUnitOfMeasure.SelectedValue = ReadTransportDataProperty(transportTypeData, "pollingUnitOfMeasure");
                    break;
                case "SQL":
                    txtConnString.Text = ReadTransportDataProperty(transportTypeData, "connectionString");
                    txtRootElementName.Text = ReadTransportDataProperty(transportTypeData, "outputRootElementName");
                    txtTargetNamespace.Text = ReadTransportDataProperty(transportTypeData, "documentTargetNamespace");
                    break;
                default:
                    break;
            }

            ToggleTransportInfoPanel(receiveLocation.TransportType);
        }
        else
            dvwReceiveLocation.ChangeMode(DetailsViewMode.Insert);
    }

    protected void dvwReceiveLocation_ModeChanged(object sender, EventArgs e)
    {
        if (dvwReceiveLocation.CurrentMode == DetailsViewMode.Insert)
        {
            DropDownList TransportType = (DropDownList)dvwReceiveLocation.FindControl("ddlTransportType");
            InitTransportInfoPanel(TransportType.SelectedValue);
            ToggleTransportInfoPanel(TransportType.SelectedValue);
        }
    }

    protected void ddlTransportType_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList TransportType = (DropDownList) sender;
        InitTransportInfoPanel(TransportType.SelectedValue);
        ToggleTransportInfoPanel(TransportType.SelectedValue);
    }

    private void InitTransportInfoPanel(string transportType)
    {
        switch (transportType)
        {
            case "FILE":
                txtFolder.Text = String.Empty;
                txtFileMask.Text = "*.xml";
                txtPollingInterval.Text = "60000";
                txtBatchSize.Text = "20";
                txtBatchSizeInBytes.Text = "102400";
                txtFileNetFailRetryInt.Text = "5";
                txtFileNetFailRetryCount.Text = "5";
                chkRemoveReceivedFiles.Checked = false;
                txtRemoveReceivedFileDelay.Text = "10";
                txtRemoveReceivedFileRetryCount.Text = "5";
                txtRemoveReceivedFileMaxInterval.Text = "300000";
                break;
            case "FTP":
                txtServerAddress.Text = String.Empty;
                txtServerPort.Text = "21";
                txtUserName.Text = String.Empty;
                txtPassword.Text = String.Empty;
                txtTargetFolder.Text = String.Empty;
                txtTargetFileName.Text = String.Empty;
                txtCommandLogFilename.Text = String.Empty;
                ddlRepresentationType.SelectedValue = "binary";
                txtSpoolingFolder.Text = String.Empty;
                txtMaximumBatchSize.Text = "0";
                txtMaximumNumberOfFiles.Text = "0";
                txtErrorThreshold.Text = "10";
                txtMaxFileSize.Text = "100";
                txtFirewallAddress.Text = String.Empty;
                txtFirewallPort.Text = "21";
                txtFirewallUser.Text = String.Empty;
                txtFirewallPassword.Text = String.Empty;
                ddlFirewallType.SelectedValue = "NoFirewall";
                ddlPassiveMode.SelectedValue = "Active";
                ddlPollingUnitOfMeasure.SelectedValue = "Seconds";
                break;
            case "SQL":
                txtConnString.Text = String.Empty;
                txtRootElementName.Text = String.Empty;
                txtTargetNamespace.Text = String.Empty;
                break;
            default:
                break;
        }
    }

    private void ToggleTransportInfoPanel(string transportType)
    {
        pnlFILEInfo.Visible = false;
        pnlFTPInfo.Visible = false;
        pnlSQLInfo.Visible = false;

        txtFolder.Enabled = false;
        txtFileMask.Enabled = false;
        txtPollingInterval.Enabled = false;
        txtBatchSize.Enabled = false;
        txtBatchSizeInBytes.Enabled = false;
        txtFileNetFailRetryInt.Enabled = false;
        txtFileNetFailRetryCount.Enabled = false;
        chkRemoveReceivedFiles.Enabled = false;
        txtRemoveReceivedFileDelay.Enabled = false;
        txtRemoveReceivedFileRetryCount.Enabled = false;
        txtRemoveReceivedFileMaxInterval.Enabled = false;

        txtServerAddress.Enabled = false;
        txtServerPort.Enabled = false;
        txtUserName.Enabled = false;
        txtPassword.Enabled = false;
        txtTargetFolder.Enabled = false;
        txtTargetFileName.Enabled = false;
        txtCommandLogFilename.Enabled = false;
        ddlRepresentationType.Enabled = false;
        txtSpoolingFolder.Enabled = false;
        txtMaximumBatchSize.Enabled = false;
        txtMaximumNumberOfFiles.Enabled = false;
        txtErrorThreshold.Enabled = false;
        txtMaxFileSize.Enabled = false;
        txtFirewallAddress.Enabled = false;
        txtFirewallPort.Enabled = false;
        txtFirewallUser.Enabled = false;
        txtFirewallPassword.Enabled = false;
        ddlFirewallType.Enabled = false;
        ddlPassiveMode.Enabled = false;
        txtPollingInt.Enabled = false;
        ddlPollingUnitOfMeasure.Enabled = false;

        txtConnString.Enabled = false;
        txtRootElementName.Enabled = false;
        txtTargetNamespace.Enabled = false;

        bool fieldEnabled = dvwReceiveLocation.CurrentMode != DetailsViewMode.ReadOnly;
        if ((dvwReceiveLocation.CurrentMode == DetailsViewMode.Edit) && (null == dvwReceiveLocation.DataItem))
            fieldEnabled = false;

        switch (transportType)
        {
            case "FILE":
                txtFolder.Enabled = fieldEnabled;
                txtFileMask.Enabled = fieldEnabled;
                txtPollingInterval.Enabled = fieldEnabled;
                txtBatchSize.Enabled = fieldEnabled;
                txtBatchSizeInBytes.Enabled = fieldEnabled;
                txtFileNetFailRetryInt.Enabled = fieldEnabled;
                txtFileNetFailRetryCount.Enabled = fieldEnabled;
                chkRemoveReceivedFiles.Enabled = fieldEnabled;
                txtRemoveReceivedFileDelay.Enabled = fieldEnabled;
                txtRemoveReceivedFileRetryCount.Enabled = fieldEnabled;
                txtRemoveReceivedFileMaxInterval.Enabled = fieldEnabled;

                pnlFILEInfo.Visible = true;
                break;
            case "FTP":
                txtServerAddress.Enabled = fieldEnabled;
                txtServerPort.Enabled = fieldEnabled;
                txtUserName.Enabled = fieldEnabled;
                txtPassword.Enabled = fieldEnabled;
                txtTargetFolder.Enabled = fieldEnabled;
                txtTargetFileName.Enabled = fieldEnabled;
                txtCommandLogFilename.Enabled = fieldEnabled;
                ddlRepresentationType.Enabled = fieldEnabled;
                txtSpoolingFolder.Enabled = fieldEnabled;
                txtMaximumBatchSize.Enabled = fieldEnabled;
                txtMaximumNumberOfFiles.Enabled = fieldEnabled;
                txtErrorThreshold.Enabled = fieldEnabled;
                txtMaxFileSize.Enabled = fieldEnabled;
                txtFirewallAddress.Enabled = fieldEnabled;
                txtFirewallPort.Enabled = fieldEnabled;
                txtFirewallUser.Enabled = fieldEnabled;
                txtFirewallPassword.Enabled = fieldEnabled;
                ddlFirewallType.Enabled = fieldEnabled;
                ddlPassiveMode.Enabled = fieldEnabled;
                txtPollingInt.Enabled = fieldEnabled;
                ddlPollingUnitOfMeasure.Enabled = fieldEnabled;

                pnlFTPInfo.Visible = true;
                break;
            case "SQL":
                txtConnString.Enabled = fieldEnabled;
                txtRootElementName.Enabled = fieldEnabled;
                txtTargetNamespace.Enabled = fieldEnabled;

                pnlSQLInfo.Visible = true;
                break;
            default:
                break;
        }
    }

    private BTReceiveTransportInfo GenerateTransportInfo(string transportType)
    {
        BTReceiveTransportInfo transportInfo = new BTReceiveTransportInfo();
        switch (transportType)
        {
            case "FILE":
                TextBox Folder = pnlFILEInfo.FindControl("txtFolder") as TextBox;
                TextBox FileMask = pnlFILEInfo.FindControl("txtFileMask") as TextBox;
                TextBox PollingInterval = pnlFILEInfo.FindControl("txtPollingInterval") as TextBox;
                TextBox BatchSize = pnlFILEInfo.FindControl("txtBatchSize") as TextBox;
                TextBox BatchSizeInBytes = pnlFILEInfo.FindControl("txtBatchSizeInBytes") as TextBox;
                TextBox FileNetFailRetryInt = pnlFILEInfo.FindControl("txtFileNetFailRetryInt") as TextBox;
                TextBox FileNetFailRetryCount = pnlFILEInfo.FindControl("txtFileNetFailRetryCount") as TextBox;
                CheckBox RemoveReceivedFiles = pnlFILEInfo.FindControl("chkRemoveReceivedFiles") as CheckBox;
                TextBox RemoveReceivedFileDelay = pnlFILEInfo.FindControl("txtRemoveReceivedFileDelay") as TextBox;
                TextBox RemoveReceivedFileRetryCount = pnlFILEInfo.FindControl("txtRemoveReceivedFileRetryCount") as TextBox;
                TextBox RemoveReceivedFileMaxInterval = pnlFILEInfo.FindControl("txtRemoveReceivedFileMaxInterval") as TextBox;

                if (null != Folder)
                    transportInfo.Folder = Folder.Text;
                if (null != FileMask)
                    transportInfo.FileMask = FileMask.Text;
                if (null != PollingInterval)
                    transportInfo.PollingInterval = Convert.ToInt32(PollingInterval.Text);
                if (null != BatchSize)
                    transportInfo.BatchSize = Convert.ToInt32(BatchSize.Text);
                if (null != BatchSizeInBytes)
                    transportInfo.BatchSizeInBytes = Convert.ToInt32(BatchSizeInBytes.Text);
                if (null != FileNetFailRetryInt)
                    transportInfo.FileNetFailRetryInt = Convert.ToInt32(FileNetFailRetryInt.Text);
                if (null != FileNetFailRetryCount)
                    transportInfo.FileNetFailRetryCount = Convert.ToInt32(FileNetFailRetryCount.Text);
                if (null != RemoveReceivedFiles)
                    transportInfo.RemoveReceivedFiles = RemoveReceivedFiles.Checked;
                if (null != RemoveReceivedFileDelay)
                    transportInfo.RemoveReceivedFileDelay = Convert.ToInt32(RemoveReceivedFileDelay.Text);
                if (null != RemoveReceivedFileRetryCount)
                    transportInfo.RemoveReceivedFileRetryCount = Convert.ToInt32(RemoveReceivedFileRetryCount.Text);
                if (null != RemoveReceivedFileMaxInterval)
                    transportInfo.RemoveReceivedFileMaxInterval = Convert.ToInt32(RemoveReceivedFileMaxInterval.Text);
                break;
            case "FTP":
                TextBox ServerAddress = pnlFTPInfo.FindControl("txtServerAddress") as TextBox;
                TextBox ServerPort = pnlFTPInfo.FindControl("txtServerPort") as TextBox;
                TextBox UserName = pnlFTPInfo.FindControl("txtUserName") as TextBox;
                TextBox Password = pnlFTPInfo.FindControl("txtPassword") as TextBox;
                TextBox TargetFolder = pnlFTPInfo.FindControl("txtTargetFolder") as TextBox;
                TextBox TargetFileName = pnlFTPInfo.FindControl("txtTargetFileName") as TextBox;
                TextBox CommandLogFilename = pnlFTPInfo.FindControl("txtCommandLogFilename") as TextBox;
                DropDownList RepresentationType = pnlFTPInfo.FindControl("ddlRepresentationType") as DropDownList;
                TextBox SpoolingFolder = pnlFTPInfo.FindControl("txtSpoolingFolder") as TextBox;
                TextBox MaximumBatchSize = pnlFTPInfo.FindControl("txtMaximumBatchSize") as TextBox;
                TextBox MaximumNumberOfFiles = pnlFTPInfo.FindControl("txtMaximumNumberOfFiles") as TextBox;
                TextBox ErrorThreshold = pnlFTPInfo.FindControl("txtErrorThreshold") as TextBox;
                TextBox MaxFileSize = pnlFTPInfo.FindControl("txtMaxFileSize") as TextBox;
                TextBox FirewallAddress = pnlFTPInfo.FindControl("txtFirewallAddress") as TextBox;
                TextBox FirewallPort = pnlFTPInfo.FindControl("txtFirewallPort") as TextBox;
                TextBox FirewallUser = pnlFTPInfo.FindControl("txtFirewallUser") as TextBox;
                TextBox FirewallPassword = pnlFTPInfo.FindControl("txtFirewallPassword") as TextBox;
                DropDownList FirewallType = pnlFTPInfo.FindControl("ddlFirewallType") as DropDownList;
                DropDownList PassiveMode = pnlFTPInfo.FindControl("ddlPassiveMode") as DropDownList;
                TextBox PollingInt = pnlFILEInfo.FindControl("txtPollingInt") as TextBox;
                DropDownList PollingUnitOfMeasure = pnlFTPInfo.FindControl("ddlPollingUnitOfMeasure") as DropDownList;

                if (null != ServerAddress)
                    transportInfo.ServerAddress = ServerAddress.Text;
                if (null != ServerPort)
                    transportInfo.ServerPort = Convert.ToInt32(ServerPort.Text);
                if (null != UserName)
                    transportInfo.UserName = UserName.Text;
                if (null != Password)
                    transportInfo.Password = Password.Text;
                if (null != RepresentationType)
                    transportInfo.RepresentationType = RepresentationType.SelectedValue;
                if (null != TargetFolder)
                    transportInfo.TargetFolder = TargetFolder.Text;
                if (null != TargetFileName)
                    transportInfo.FileMask = TargetFileName.Text;
                if (null != CommandLogFilename)
                    transportInfo.CommandLogFilename = CommandLogFilename.Text;
                if (null != SpoolingFolder)
                    transportInfo.SpoolingFolder = SpoolingFolder.Text;
                if (null != MaximumBatchSize)
                    transportInfo.MaximumBatchSize = Convert.ToInt32(MaximumBatchSize.Text);
                if (null != MaximumNumberOfFiles)
                    transportInfo.MaximumNumberOfFiles = Convert.ToInt32(MaximumNumberOfFiles.Text);
                if (null != ErrorThreshold)
                    transportInfo.ErrorThreshold = Convert.ToInt32(ErrorThreshold.Text);
                if (null != MaxFileSize)
                    transportInfo.MaxFileSize = Convert.ToInt32(MaxFileSize.Text);
                if (null != FirewallAddress)
                    transportInfo.FirewallAddress = FirewallAddress.Text;
                if (null != FirewallPort)
                    transportInfo.FirewallPort = Convert.ToInt32(FirewallPort.Text);
                if (null != FirewallUser)
                    transportInfo.FirewallUser = UserName.Text;
                if (null != FirewallPassword)
                    transportInfo.FirewallPassword = Password.Text;
                if (null != FirewallType)
                    transportInfo.FirewallType = FirewallType.SelectedValue;
                if (null != PassiveMode)
                    transportInfo.PassiveMode = PassiveMode.SelectedValue == "Passive";
                if (null != PollingInt)
                    transportInfo.PollingInterval = Convert.ToInt32(PollingInt.Text);
                if (null != PollingUnitOfMeasure)
                    transportInfo.PollingUnitOfMeasure = PollingUnitOfMeasure.SelectedValue;
                break;
            case "SQL":
                TextBox ConnString = pnlSQLInfo.FindControl("txtConnString") as TextBox;
                TextBox RootElementName = pnlSQLInfo.FindControl("txtRootElementName") as TextBox;
                TextBox TargetNamespace = pnlSQLInfo.FindControl("txtTargetNamespace") as TextBox;

                if (null != ConnString)
                    transportInfo.ConnectionString = ConnString.Text;
                if (null != RootElementName)
                    transportInfo.OutputRootElementName = RootElementName.Text;
                if (null != TargetNamespace)
                    transportInfo.DocumentTargetNamespace = TargetNamespace.Text;
                break;
            default:
                break;
        }

        return transportInfo;
    }

    private static string ReadTransportDataProperty(string transportData, string propertyName)
    {
        try
        {
            XmlDocument configXml = new XmlDocument();
            configXml.LoadXml(transportData);
            XmlNode propertyNode = configXml.SelectSingleNode("//" + propertyName);
            if (null == propertyNode)
            {
                int start = transportData.IndexOf("&lt;" + propertyName + "&gt;") + propertyName.Length + 8;
                int end = transportData.IndexOf("&lt;/" + propertyName + "&gt;");
                int length = end - start;

                return (length > 0) ? transportData.Substring(start, length) : String.Empty;
            }
            else
                return propertyNode.InnerText;
        }
        catch
        {
            int start = transportData.IndexOf("&lt;" + propertyName + "&gt;") + propertyName.Length + 8;
            int end = transportData.IndexOf("&lt;/" + propertyName + "&gt;");
            int length = end - start;

            return (length > 0) ? transportData.Substring(start, length) : String.Empty;
        }
    }
}